package com.ruoyi.environment.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.environment.domain.OpmEnvEquipment;
import com.ruoyi.environment.service.IOpmEnvEquipmentService;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 园区设备Controller
 *
 * @author cjie
 * @date 2021-07-23
 */
@Api(tags = " 园区设备")
@RestController
@RequestMapping("/opmEnvEquipment")
public class OpmEnvEquipmentController extends BaseController {
    @Autowired
    private IOpmEnvEquipmentService opmEnvEquipmentService;

    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 查询园区设备列表
     */
    @ApiOperation(value="查询园区设备列表", notes="查询园区设备列表")
    @PreAuthorize(hasPermi = "environment:opmEnvEquipment:list")
    @GetMapping("/list")
    public TableDataInfo list(OpmEnvEquipment opmEnvEquipment) {
        startPage();
        List<OpmEnvEquipment> list = opmEnvEquipmentService.selectOpmEnvEquipmentList(opmEnvEquipment);
        return getDataTable(list);
    }

    /**
     * 导出园区设备列表
     */
    @ApiOperation(value="导出园区设备列表", notes="导出园区设备列表")
    @PreAuthorize(hasPermi = "environment:opmEnvEquipment:export")
    @Log(title = "园区设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OpmEnvEquipment opmEnvEquipment) throws IOException {
        List<OpmEnvEquipment> list = opmEnvEquipmentService.selectOpmEnvEquipmentList(opmEnvEquipment);
        ExcelUtil<OpmEnvEquipment> util = new ExcelUtil<OpmEnvEquipment>(OpmEnvEquipment.class);
        util.exportExcel(response, list, "园区设备数据");
    }
    /**
     * 获取园区设备详细信息
     */
    @ApiOperation(value="获取园区设备详细信息", notes="获取园区设备详细信息")
    @PreAuthorize(hasPermi = "environment:opmEnvEquipment:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(opmEnvEquipmentService.selectOpmEnvEquipmentById(id));
    }

    /**
     * 新增园区设备
     */
    @ApiOperation(value="新增园区设备", notes="新增园区设备")
    @PreAuthorize(hasPermi = "environment:opmEnvEquipment:add")
    @Log(title = "园区设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OpmEnvEquipment opmEnvEquipment) {
        RemoteSysBmType remoteSysBmType= remoteBmCodeService.genCode(opmEnvEquipment.getSysBmId(), SysBmCodeTypeValue.DEVICE);
        String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
        String deviceType = remoteSysBmType.getHierarchy();//层级
        opmEnvEquipment.setFacilityNum(nowCode);
        opmEnvEquipment.setDeviceType(deviceType);
        return toAjax(opmEnvEquipmentService.insertOpmEnvEquipment(opmEnvEquipment));
    }

    /**
     * 修改园区设备
     */
    @ApiOperation(value="修改园区设备", notes="修改园区设备")
    @PreAuthorize(hasPermi = "environment:opmEnvEquipment:edit")
    @Log(title = "园区设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OpmEnvEquipment opmEnvEquipment) {
        return toAjax(opmEnvEquipmentService.updateOpmEnvEquipment(opmEnvEquipment));
    }

    /**
     * 删除园区设备
     */
    @ApiOperation(value="删除园区设备", notes="删除园区设备")
    @ApiImplicitParam(name = "ids", value = "园区设备ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "environment:opmEnvEquipment:remove")
    @Log(title = "园区设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(opmEnvEquipmentService.deleteOpmEnvEquipmentByIds(ids));
    }
}
